import unittest  # 导入unittest模块，用于单元测试
import requests  # 导入requests库，用于发送HTTP请求

class TestAPI(unittest.TestCase):
    """
    接口自动化测试类
    """
    def test_baidu_home(self):
        """
        测试百度首页是否可以正常访问
        """
        url = 'https://www.baidu.com/'
        try:
            response = requests.get(url, timeout=5)
            self.assertEqual(response.status_code, 200)  # 断言状态码为200
            self.assertIn('百度', response.text)  # 断言返回内容包含"百度"
        except Exception as e:
            self.fail(f'请求失败：{e}')

    def test_api_example(self):
        """
        测试一个示例API接口（请替换为实际接口）
        """
        url = 'https://jsonplaceholder.typicode.com/posts/1'
        try:
            response = requests.get(url, timeout=5)
            self.assertEqual(response.status_code, 200)
            data = response.json()
            self.assertEqual(data['id'], 1)
        except Exception as e:
            self.fail(f'API请求失败：{e}')

if __name__ == '__main__':
    unittest.main() 